<?php

namespace app\stores\controller\miniapp;

use app\BaseController;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\partner\PartnerBonusModel;
use app\beautiful\model\partner\PartnerCommissionModel;
use app\beautiful\model\PartnerModel;
use app\common\model\MemberConsumeModel;


class Partner extends BaseController{

    public function index(){
        $member=$this->request->member;
        $shop_id = $this->request->param('shop_id',0);
        $where=[];
        $where['member_id']=$member['member_id'];
        if(!empty($shop_id)){
            $where['shop_id']=$shop_id;
        }
        $order['a.partner_id']='desc';
        $list=PartnerModel::list($where,$order);
        return $this->success($list);
    }
    public function info(){
        $member=$this->request->member;
        $shop_id = $this->request->param('shop_id',0);
        $partner_id = $this->request->param('partner_id',0);
        $where=[];
        $where['member_id']=$member['member_id'];
        if(!empty($shop_id)){
            $where['shop_id']=$shop_id;
        }
        if(!empty($shop_id)){
            $where['partner_id']=$partner_id;
        }
        $order['a.partner_id']='desc';
        $info=PartnerModel::info($where,$order);
        if(!empty($info)){
            $info['profit']=PartnerCommissionModel::where(['partner_id'=>$partner_id])->sum('commission');
        }
        return $this->success($info);
    }
    public function pro_list(){
        $member=$this->request->member;
        $limit=$this->request->limit;
        $page=$this->request->page;
        $partner_id = $this->request->param('partner_id',0);
        if(empty($partner_id)){
            return $this->error('partner_id参数错误');
        }
        $partner_info=PartnerModel::where(['partner_id'=>$partner_id,'member_id'=>$member['member_id']])->find();
        if(empty($partner_info)){
            return $this->error('股东信息不存在！');
        }
        $where=[];
        $where['partner_id']=$partner_info['partner_id'];
        $where['shop_id']=$partner_info['shop_id'];
        $order['a.commission_id']='desc';
        $res_data=PartnerCommissionModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function member_list(){
        $member=$this->request->member;
        $limit=$this->request->limit;
        $page=$this->request->page;
        $partner_id = $this->request->param('partner_id',0);
        if(empty($partner_id)){
            return $this->error('partner_id参数错误');
        }
        $partner_info=PartnerModel::where(['partner_id'=>$partner_id,'member_id'=>$member['member_id']])->find();
        if(empty($partner_info)){
            return $this->error('股东信息不存在！');
        }
        $where=[];
        $where['partner_id']=$partner_info['partner_id'];
        $where['status']=1;
        $order['a.member_id']='desc';
        $res_data=MemberModel::list($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function list(){
        $member=$this->request->member;
        $limit=$this->request->limit;
        $page=$this->request->page;
        $status = $this->request->param('status',1);
        $partner_id = $this->request->param('partner_id',0);
        if($partner_id==0) return $this->error('参数错误');
        $partner_info=PartnerModel::where(['partner_id'=>$partner_id,'member_id'=>$member['member_id']])->find();
        if(empty($partner_info)) return $this->error('股东信息不正确');
        $where=[];
        $where['partner_id']=$partner_id;
        $where['status']=$status;
        $order['a.partner_bonus_id']='desc';
        $res_data=PartnerBonusModel::ulist($where,$order,$page,$limit);
        $all_reward=PartnerBonusModel::where($where)->sum('bonus_price');
        $res_data['all_reward']=$all_reward;
        return $this->success($res_data);
    }
}

